package cn.itcast.wushangxiantu.BaseAdapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.UserData.FenZhengData;

public class FenZhengGuoJiaAdapter extends BaseAdapter {
    private FenZhengData data;
    private Context context;
    private ViewHolder viewHolder;

    public FenZhengGuoJiaAdapter(Context context) {
        this.context = context;
    }

    public FenZhengData getData() {
        return data;
    }

    public void setData(FenZhengData data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.getGuoJiaZhanChang().size();
    }

    @Override
    public Object getItem(int i) {
        return data.getGuoJiaZhanChang().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.adapter_fenzheng_guojia, null);
        viewHolder = new ViewHolder(view);
        viewHolder.tvDj.setText("LV：" + data.getGuoJiaZhanChang().get(i).getDj());
        viewHolder.tvName.setText(data.getGuoJiaZhanChang().get(i).getName());
        viewHolder.tvSy.setText("收益：" + data.getGuoJiaZhanChang().get(i).getGjsy());
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newTime = formatter.format(currentTime); // 当前本地时间
        long OTime = 0;
        long NTime = 0;
        try {
            OTime = formatter.parse(data.getGuoJiaZhanChang().get(i).getTime()).getTime();
            NTime = formatter.parse(newTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long diff = (NTime - OTime);
        if (diff>604800000){
            viewHolder.tvName.setTextColor(ContextCompat.getColor(context,R.color.wuqi4));
        }else {
            viewHolder.tvName.setTextColor(ContextCompat.getColor(context,R.color.wuqi0));
        }
        return view;
    }

    class ViewHolder {
        public View rootView;
        public TextView tvName;
        public TextView tvDj;
        public TextView tvSy;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tvName = (TextView) rootView.findViewById(R.id.tvName);
            this.tvDj = (TextView) rootView.findViewById(R.id.tvDj);
            this.tvSy = (TextView) rootView.findViewById(R.id.tvSy);
        }

    }
}
